//
//  ViewController.m
//  MoreViewCDemo
//
//  Created by luming on 2018/6/20.
//  Copyright © 2018年 luming. All rights reserved.
//

#import "ViewController.h"
#import "AViewController.h"
#import "BViewController.h"
#import "CViewController.h"
#import "DViewController.h"
#import "EViewController.h"
#import "FViewController.h"
#import "LMTitlesScrollView.h"
#define ScreenWidth                         [[UIScreen mainScreen] bounds].size.width
#define ScreenHeight                        [[UIScreen mainScreen] bounds].size.height


@interface ViewController ()<UIScrollViewDelegate,LMTitlesScrollViewDelegate>

@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) LMTitlesScrollView *titleScrollView;

@property(nonatomic,strong)NSArray *titles;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.titleScrollView];

    [self.view addSubview:self.scrollView];
    
    _scrollView.contentSize = CGSizeMake(6*ScreenWidth, 0);
    
    [self addChildVC:[AViewController new] frame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    [self addChildVC:[BViewController new] frame:CGRectMake(ScreenWidth, 0, ScreenWidth, ScreenHeight)];
     [self addChildVC:[CViewController new] frame:CGRectMake(2*ScreenWidth, 0, ScreenWidth, ScreenHeight)];
     [self addChildVC:[DViewController new] frame:CGRectMake(3*ScreenWidth, 0, ScreenWidth, ScreenHeight)];
    [self addChildVC:[EViewController new] frame:CGRectMake(4*ScreenWidth, 0, ScreenWidth, ScreenHeight)];

    [self addChildVC:[FViewController new] frame:CGRectMake(5*ScreenWidth, 0, ScreenWidth, ScreenHeight)];

}

- (void)addChildVC:(UIViewController *)vc frame:(CGRect)frame{
    [self addChildViewController:vc];
    [self.scrollView addSubview:vc.view];
    vc.view.frame = frame;
    [vc didMoveToParentViewController:self];
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.scrollView) {
        int index = self.scrollView.contentOffset.x / ScreenWidth;

        self.scrollView.contentOffset = CGPointMake(index * ScreenWidth, self.scrollView.contentOffset.y);
        _titleScrollView.currentIndex = index;
    }
}

#pragma mark - LMScrollview Delegate
- (void)titlesScrollViewDidClicked:(NSInteger)index
{
        self.scrollView.contentOffset = CGPointMake(index * ScreenWidth, self.scrollView.contentOffset.y);
}


- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64)];
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        
    }
    return _scrollView;
}

- (LMTitlesScrollView *)titleScrollView
{
    if (!_titleScrollView) {
        _titles = @[@"A控制器",@"B控制器",@"C控制器",@"D控制器",@"E控制器",@"F控制器"];
        _titleScrollView = [[LMTitlesScrollView alloc] initWithFrame:CGRectMake(0, 20, ScreenWidth, 44) titles:_titles delegate:self];
    }
    return _titleScrollView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
